// MigDb.cpp : Defines the entry point for the console application.
//

#include <Common/include.h>
#include <Database/Database.h>
#include <Database/Tables/TableNames.h>

void to_0_2_0(CDatabase* pDb);
void to_0_3_0(CDatabase* pDb);

int _tmain(int argc, _TCHAR* argv[])
{
	std::tstring strVer = _T("0.1.1");
	CDatabase db(JOURNAL_DATABASE_NAME);
	db.Open();
	try{
		db.ExecRequest(_T("BEGIN TRANSACTION"));
		if(db.IsExistTable(VERSION_DB_TABLE_NAME)){
			std::tstring strRequest(_T("SELECT version FROM "));
			MyTable table = db.ExecRequest(strRequest + VERSION_DB_TABLE_NAME);
			strVer = table[0][0].m_strValueColumn;
		}else{
			std::tstring strRequest(_T("CREATE TABLE "));
			db.ExecRequest(strRequest + VERSION_DB_TABLE_NAME + _T("(version TEXT NOT NULL);"));
			strRequest = _T("INSERT INTO ");
			db.ExecRequest(strRequest + VERSION_DB_TABLE_NAME + _T(" VALUES('0.1.1');"));
		}
		if(strVer == _T("0.1.1")){
			printf("modify database to 0.2.0\n");
			to_0_2_0(&db);
			strVer = _T("0.2.0");
		}
		if(strVer == _T("0.2.0")){
			printf("modify database to 0.3.0\n");
			to_0_3_0(&db);
			strVer = _T("0.3.0");
		}
		std::tstring strRequest(_T("UPDATE "));
		db.ExecRequest(strRequest + VERSION_DB_TABLE_NAME + _T(" SET version='") + strVer + _T("';"));
		db.ExecRequest(_T("END TRANSACTION"));
	}catch(CBaseError& error){
		AfxMessageBox(error.GetError().c_str());
	}
	return 0;
}

